package com.my.serviceimpl;

import com.my.bean.OrderVip;
import com.my.bean.ResultPage;
import com.my.bean.TrVip;
import com.my.bean.VoPage;
import com.my.mapper.TrVipMapper;
import com.my.service.TrVipService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2018-09-14
 */
@Service
public class TrVipServiceImpl extends ServiceImpl<TrVipMapper, TrVip> implements TrVipService {


    /**
     * 实现分页查询
     * @param map
     * @param voPage
     * @return
     */
    @Override
    public ResultPage<TrVip> selectMyPage(Map map, VoPage voPage,Long areaid,boolean admin) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<TrVip> list = baseMapper.selectMyPage(map,paramMap,areaid,admin);
        Integer count = baseMapper.countMyPage(map,paramMap,areaid,admin);
        ResultPage<TrVip> resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }



    /**
     * 修改
     * @param id
     * @return
     */
    public List<TrVip> selectTrVipById(Long id){
        return  baseMapper.selectTrVipById(id);
    }


    /**
     * 根据状态查询
     * @param id
     * @return
     */
    @Override
    public String updateTrState(Long id) {
        if(baseMapper.updateTrState(id)>0){
            return  "修改成功";
        }
        return  "修改失败";

    }




    /**
     * 封装成Map  用mybatis plus
     * 查询房间的订单
     * @param
     * @return
     */
    @Override
    public String selectByHomeId(TrVip trVip) {
        //先封装成一个Map
        Map map = new HashMap();
        //里边写传参数,一个是房间的id 一个是退房的字段,1是退房2是未退房
        map.put("home_id", trVip.getHomeId());
        //查出来的是key是数据库的字段,2是未退房
        map.put("whether_home", 2);
        //把传进去的id和退房的字段,  this在serviceImpl里边值的是mapper,在Mybatis plus插件中;
        //this指在本类中已经注入了mapper,直接用this就行,不用注入Mapper
        //调用Mybatis plus中的selectMap方法把map传进去,是一个集合
        //List<TrVip> list = this.selectByMap(map);
        //如果查出来的订单大于0
        //if (list.size() > 0) {
            //就返回该房间已经有订单,
           // return "该房间已经有订单";
            //否则 就添加
        //} else {
            //添加订单 默认是状态是2未完成
            trVip.setOrderStatus(2);
            trVip.setWhetherHome(2);
            if (this.insert(trVip)) {
                return "保存成功";
            } else {
                return "保存失败";
            }
        //}
    }















}
